#ifndef CSAPP_H_
#define CSAPP_H_
#include <stdio.h>
#include <ctime>
#include <algorithm>
using namespace std;
typedef unsigned char *byte_pointer;

void show_binary(byte_pointer start, int len){
    for(int i = len - 1; i >= 0; i--){
        int b = 1 << 7;
        for(int k = 0; k < 8; k++){
            if((start[i] & b) > 0){
                printf("1");
            }else{
                printf("0");
            }
            b >>= 1;
        }
    }
    printf("\n");
}

void show_bytes(byte_pointer start, int len){
    for(int i = len - 1; i >= 0; i--){
        printf("%.2x", start[i]);
    }
    printf("\n");
}

template<typename T>
void show_bytes(T x){
    show_bytes((byte_pointer)&x, sizeof(T));
}

template<typename T>
void show_binary(T x){
    show_binary((byte_pointer)&x, sizeof(T));
}

// 测试循环展开
void test_loop_unroll(){
    srand(time(0));
    const int n = 1<<15;
    int a[n], b[n], min[n], max[n]; 
    int i, t;
    for(i = 0; i < n; i++){
        a[i] = rand() % 256;
        b[i] = rand() % 256;
    }
    const int cnt = 20000;
    long long sum = 0;
    clock_t t0 = clock();
    for(t = 0; t < cnt; t++){
        for(i = 0; i < n; i++){
            if(a[i] > b[i]){
                min[i] = b[i];
                max[i] = a[i];
            }else{
                min[i] = a[i];
                max[i] = b[i];
            }
        }
    }
    clock_t t1 = clock();
    double time_cost1 = static_cast<double>(t1 - t0) / CLOCKS_PER_SEC;
    cout << "time_cost1 = " << time_cost1 << endl;
    sort(a, a + n);
    sum = 0;
    t1 = clock();
    for(t = 0; t < cnt; t++){
        for(i = 0; i < n; i++){
            int minv = a[i] < b[i] ? a[i] : b[i];
            int maxv = a[i] > b[i] ? a[i] : b[i];
            min[i] = minv;
            max[i] = maxv;
        }
    }
    clock_t t2 = clock();
    double time_cost2 = static_cast<double>(t2 - t1) / CLOCKS_PER_SEC;
    cout << "time_cost2 = " << time_cost2 << endl;
}

#endif
